import {
	IParam,
	IViewActionResult,
	IWizardPanelAbility,
	IWizardViewAbility,
	IWizardViewController,
	IWizardViewStore,
	WizardViewActionType,
} from '@/core/interface';
import { DEViewController } from './de-view-controller';

/**
 * 向导视图控制器
 *
 * @export
 * @class WizardViewController
 * @extends {MDViewController<WizardViewActionType, IWizardViewStore, IWizardViewAbility>}
 * @implements {IWizardViewController<IWizardViewStore, IWizardViewAbility>}
 */
export class WizardViewController
	extends DEViewController<
		WizardViewActionType,
		IWizardViewStore,
		IWizardViewAbility
	>
	implements IWizardViewController<IWizardViewStore, IWizardViewAbility>
{
	/**
	 * 获取向导部件能力
	 *
	 * @protected
	 * @return {*}  {(IWizardPanelAbility | undefined)}
	 * @memberof WizardViewController
	 */
	protected getMainCtrlAbility(): IWizardPanelAbility | undefined {
		const wizard = this.model.ctrls.find(
			(ctrl: IParam) => ctrl.controlType === 'WIZARDPANEL'
		);
		if (wizard) {
			return this.getSubAbility<IWizardPanelAbility>(wizard.name);
		}
		return undefined;
	}

	/**
	 * 视图挂载
	 *
	 * @param {(IParam | undefined)} [opts]
	 * @return {*}  {void}
	 * @memberof WizardViewController
	 */
	public viewMounted(opts: IParam = {}): void {
		super.viewMounted(opts);
		if (this.model.useDefaultLayout) {
            if (!this.isLoadDefault) {
				this.isLoadDefault = true;
				return;
			}
			this.load();
		} else {
			this.initLayout().then(() => {
                if (!this.isLoadDefault) {
                    this.isLoadDefault = true;
                    return;
                }
				this.load();
			});
		}
	}

	/**
	 * 加载视图
	 *
	 * @memberof DEViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		const ability = this.getMainCtrlAbility();
		if (ability) {
			return ability.load(args);
		}
		return Promise.resolve({ ok: false, rowData: {}, data: [] });
	}
}
